package ru.voronoy.thefifteen.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

import ru.voronoy.thefifteen.config.BoneConf;
import ru.voronoy.thefifteen.entity.Bone;
import ru.voronoy.thefifteen.gameplay.Game;
import ru.voronoy.thefifteen.gameplay.TimerPrinter;
import ru.voronoy.thefifteen.listeners.BonesKeyListener;

public class MainWindow extends JFrame {
	private static Map<String, Bone> bones = Game.getInstance().Locate();
	private static JPanel bonesPanel = new JPanel();
	private static Timer timer;
	private static JLabel timerLabel;

	public MainWindow() {
		setTitle("Пятнашки");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLayout(new BorderLayout());
		setLocationByPlatform(true);

		bonesPanel.setSize(BONESPANEL_WIDTH, BONESPANEL_HEIGHT);
		bonesPanel.setLayout(null);
		bonesPanel.setBorder(new LineBorder(BoneConf.getInstance().BorderColor,
				BoneConf.getInstance().BorderThikness,
				BoneConf.getInstance().RoundCorners));
		add(bonesPanel);

		JPanel controlPanel = new JPanel();
		controlPanel.setSize(DEFAULT_WIDTH - BONESPANEL_WIDTH, DEFAULT_HEIGHT
				- BONESPANEL_HEIGHT);
		controlPanel.setLayout(new BorderLayout());
		add(controlPanel, BorderLayout.SOUTH);

		timerLabel = new JLabel("00:00:00");
		timerLabel.setSize(50, 10);
		controlPanel.add(timerLabel, BorderLayout.CENTER);

		// создание меню
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menu = new JMenu("Игра");
		menuBar.add(menu);

		addKeyListener(new BonesKeyListener(bones));

		JMenuItem startItem = new JMenuItem("Новая игра");
		menu.add(startItem);
		menu.addSeparator();
		startItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (!Game.getInstance().isStarted()) {
					Game.getInstance().Start(bones);
					timer = new Timer(1000, new TimerPrinter(timerLabel));
					timer.start();
					repaint();
				} else {
					Game.getInstance().Stop();
					Game.getInstance().Start(bones);
					timer = new Timer(1000, new TimerPrinter(timerLabel));
					timer.start();
					repaint();
				}
			}
		});

		JMenuItem recordsItem = new JMenuItem("Рекорды");
		menu.add(recordsItem);
		recordsItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				RecordsFrame();
			}
		});

		JMenuItem exitItem = new JMenuItem("Выход");
		menu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});

		Game.getInstance().ViewBones(bonesPanel, bones);

	}

	public static void FinishFrame(String time) {
		StringBuilder builder = new StringBuilder();
		builder.append("Игра успешно завершена!!! Ваше время ");
		builder.append(time);
		builder.append('\n');
		builder.append(" Желаете сохранить результат?");
		int selection = JOptionPane.showConfirmDialog(null, builder.toString(),
				"Поздравляем!", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.INFORMATION_MESSAGE);
		if (selection == JOptionPane.OK_OPTION) {
			JFrame nameFrame = new NameFrame(timerLabel.getText());
			nameFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			nameFrame.setVisible(true);
		}
	}

	public static void RecordsFrame() {
		JFrame recordsFrame = new RecordsFrame();
		recordsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		recordsFrame.setVisible(true);
	}

	public static Map<String, Bone> getBones() {
		return bones;
	}

	public static String getTimerLabelText() {
		return timerLabel.getText();
	}

	public static void StopTimer() {
		timer.stop();
	}

	private static final int DEFAULT_WIDTH = 417;
	private static final int DEFAULT_HEIGHT = 478;
	private static final int BONESPANEL_HEIGHT = 460;
	private static final int BONESPANEL_WIDTH = 417;
}
